﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using Microsoft.SharePoint.Search.Query;
using Microsoft.SharePoint;

namespace PS.WebParts.Rollup.Search
{
    public class SearchDataView : ARollupDataSourceView
    {
        SearchDataSource searchDS;

        public SearchDataView(HttpContext context, IDataSource owner, string viewName)
            : base(context, owner, viewName)
        {
            searchDS = (SearchDataSource)owner;
        }
     
        public override DataTable GetData()
        {
            DataTable result = new DataTable();

            try
            {                
                searchDS.SearchInstruction.Run();

                this.LogError("This is NOT an Exception: MOSS Search:" + searchDS.SearchInstruction.sql);
            }
            catch (Exception ex)
            {
                this.LogError("MOSS Search:" + searchDS.SearchInstruction.sql);

                LogError("Error Searching", ex);
            }

            return searchDS.SearchInstruction.SearchResult;
        }

        public override void Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
        {
            base.Select(arguments, callback);
        }

        protected override System.Collections.IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
        {
            return base.ExecuteSelect(arguments);
        }

        public override System.Data.DataTable RollUpData(Microsoft.SharePoint.SPWeb web)
        {
            DataTable result = new DataTable();
            
            int count = this.Fields.Count;
            
            //searchDS.Fields.ToList().ForEach(a => result.Columns.Add(a));
            result.Columns.Add("Title");

            DataRow row = result.NewRow();

            row["Title"] = "testc 1";
            
            result.Rows.Add(row);

            result.AcceptChanges();

            return result;
        }
    }
}